<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<html>
	<head>
	    <title>首页</title>
	    <link href="${ctx}/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
        <link href="${ctx}/lib/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />



	</head>
	<body>
        <div id="toptoolbar"></div>
        <div id="maingrid"></div>

        <script src="${ctx}/lib/jquery/jquery-1.9.0.min.js"></script>
        <script src="${ctx}/lib/ligerUI/js/ligerui.all.js"></script>
        <script>
            var gridManager = $( "#maingrid" ).ligerGrid({
                columns: [
                    { display: "ID", name: "id", align: "center", width: 60 },
                    { display: "Last Name", name: "lastName", align: "center", width: 200 },
                    { display: "First Name", name: "firstName", align: "center", width: 200 },
                    { display: "Sex", name: "sex", align: "center", width: 200, render: function(item) { return item.sex==true ? "男" : "女"; } }
                ],
                width: "100%",
                height: "90%",
                pageSize: 20,
                checkbox: true,
                rownumbers: true,
                dataAction: "server",
                url: "${ctx}/grid.action",
                onReload: reload
            });

            $("#toptoolbar").ligerToolBar({ items: [
                { text: '增加', click: add , icon:'add'},
                { line:true },
                { text: '修改', click: edit, icon:'edit' },
                { line:true },
                { text: '删除', click: del, icon: 'delete' }
                ]
            });

            function add() {
                $.ligerDialog.open({ title:'新增', url: '${ctx}/pages/person/form.jsp', height: 400, width: 500, buttons: [
                            { text: '保存', onclick: function (item, dialog) { dialog.frame.save(); }, cls:'l-dialog-btn-highlight' },
                            { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
                         ], isResize: true, slide: false
                        });
            }

            function edit() {
                var rowdata = gridManager.getSelectedRow();
                                if ( rowdata == undefined) {
                                    $.ligerDialog.warn("请选择数据！");
                                    return;
                                }
                var id = rowdata.id;

                $.ligerDialog.open({ title:'修改', url: 'update.action?id='+id, height: 400, width: 500, buttons: [
                                        { text: '保存', onclick: function (item, dialog) { dialog.frame.save(); }, cls:'l-dialog-btn-highlight' },
                                        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
                                     ], isResize: true, slide: false, id: id

                                    });

            }

            function del() {
                var rowdata = gridManager.getSelectedRow();
                if ( rowdata == undefined) {
                    $.ligerDialog.warn("请选择数据！");
                    return;
                }
                var id = rowdata.id;
                //alert(id);
                $.ligerDialog.confirm('确定删除数据吗？', function (yes) {
                    //alert(yes);
                    if (yes) {
                        $.ajax({
                            type: "POST",
                            data: { id: id},
                            url: "${ctx}/delete.action",
                            success: function(data) {
                                if (data.code == 200) {
                                    reload();
                                    $.ligerDialog.tip({ content:"操作成功！" });
                                    setTimeout(function() {
                                        $.ligerDialog.close();
                                    }, 1000);
                                }
                            },
                            error: function() {

                            }
                        });
                    }
                });
            }


            function reload() {
                console.log("--- 刷新数据 ---");
                var params = {};
                gridManager.setOptions({
                    dataAction: "server",
                    url: "${ctx}/grid.action",
                    parms: params
                });
            }
        </script>
	</body>
</html>
